import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Admin
 * Date: 2022-05-30
 * Time: 21:51
 */
public class TestDemo {
    public static void merge(int[] array,int left,int mid, int right) {
        int s1 = left;
        int s2 = mid;
        int e1 = mid+1;
        int e2 = right;

        int[] tmpArr = new int[right-left+1];
        int k = 0;

        while(s1 <= s2 && e1 <= e2) {
            if(array[s1] < array[e1]) {
                tmpArr[k++] = array[s1++];
            } else {
                tmpArr[k++] = array[e1++];
            }
        }

        while(s1 <= s2) {
            tmpArr[k++] = array[s1++];
        }

        while(e1 <= e2) {
            tmpArr[k++] = array[e1++];
        }

        for (int i = 0; i < k; i++) {
             array[i+left] = tmpArr[i];
        }
    }

    public static void mergeSort(int[] array,int left,int right) {
        if(left >= right) return;

        int mid = (left+right)/2;

        mergeSort(array, left, mid);
        mergeSort(array, mid+1,right );

        //合并
        merge(array,left,mid,right)
    public static void main(String[] args) {
        int[] array = {1,8,2,10,22,5,33,11};
        mergeSort(array,0,array.length-1);
        System.out.println(Arrays.toString(array));
    }
}
